# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_pyname=ndg_httpsclient
_realname=ndg-httpsclient
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python2-${_realname}" "${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=0.5.1
pkgrel=3
pkgdesc="Provides enhanced HTTPS support for httplib and urllib2 using PyOpenSSL (mingw-w64)"
url='https://pypi.python.org/pypi/ndg-httpsclient'
license=('BSD')
arch=('any')
makedepends=("${MINGW_PACKAGE_PREFIX}-python2-setuptools"
             "${MINGW_PACKAGE_PREFIX}-python3-setuptools"
             "${MINGW_PACKAGE_PREFIX}-python2-pyasn1"
             "${MINGW_PACKAGE_PREFIX}-python3-pyasn1"
             "${MINGW_PACKAGE_PREFIX}-python2-pyopenssl"
             "${MINGW_PACKAGE_PREFIX}-python3-pyopenssl")
source=("${_realname}-${pkgver}.tar.gz"::"https://files.pythonhosted.org/packages/source/n/${_realname}/${_pyname}-${pkgver}.tar.gz")
sha256sums=('d72faed0376ab039736c2ba12e30695e2788c4aa569c9c3e3d72131de2592210')

prepare() {
  cd ${srcdir}
  cp -r ${_pyname}-${pkgver} python2-build-${CARCH}
  cp -r ${_pyname}-${pkgver} python3-build-${CARCH}
}

build() {
  cd "${srcdir}"
  for pver in {2,3}; do  
    msg "Python ${pver} build for ${CARCH}"  
    cd "${srcdir}/python${pver}-build-${CARCH}"
    ${MINGW_PREFIX}/bin/python${pver} setup.py build
  done
}

package_python3-ndg-httpsclient() {
  depends=("${MINGW_PACKAGE_PREFIX}-python3-pyopenssl"
           "${MINGW_PACKAGE_PREFIX}-python3-pyasn1")

  cd ${srcdir}/python3-build-${CARCH}
  ${MINGW_PREFIX}/bin/python3 setup.py build
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python3 setup.py install --prefix=${MINGW_PREFIX} --root="${pkgdir}" --optimize=1
  # fix python command in files
  local PREFIX_WIN=$(cygpath -am ${MINGW_PREFIX})
  for _f in "${pkgdir}${MINGW_PREFIX}"/bin/*.py; do
    sed -e "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i ${_f}
  done
}

package_python2-ndg-httpsclient() {
  depends=("${MINGW_PACKAGE_PREFIX}-python2-pyopenssl"
           "${MINGW_PACKAGE_PREFIX}-python2-pyasn1")

  cd ${srcdir}/python2-build-${CARCH}
  ${MINGW_PREFIX}/bin/python2 setup.py build
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python2 setup.py install --prefix=${MINGW_PREFIX} --root="${pkgdir}" --optimize=1

  mv "${pkgdir}${MINGW_PREFIX}"/bin/ndg_httpclient{,2}.exe
  mv "${pkgdir}${MINGW_PREFIX}"/bin/ndg_httpclient{,2}-script.py
  if [ -e ${pkgdir}${MINGW_PREFIX}/bin/ndg_httpclient.exe.manifest ]
  then
    mv "${pkgdir}${MINGW_PREFIX}"/bin/ndg_httpclient{,2}.exe.manifest
  fi
  # fix python command in files
   local PREFIX_WIN=$(cygpath -am ${MINGW_PREFIX})
  for _f in "${pkgdir}${MINGW_PREFIX}"/bin/*.py; do
    sed -e "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i ${_f}
  done
}

package_mingw-w64-i686-python2-ndg-httpsclient() {
  install=${_realname}2-${CARCH}.install
  package_python2-ndg-httpsclient
}

package_mingw-w64-i686-python3-ndg-httpsclient() {
  install=${_realname}3-${CARCH}.install
  package_python3-ndg-httpsclient
}

package_mingw-w64-x86_64-python2-ndg-httpsclient() {
  install=${_realname}2-${CARCH}.install
  package_python2-ndg-httpsclient
}

package_mingw-w64-x86_64-python3-ndg-httpsclient() {
  install=${_realname}3-${CARCH}.install
  package_python3-ndg-httpsclient
}
